package roleMenu

import (
	"strconv"

	"github.com/gin-gonic/gin"

	"github.com/super-team/start-kit/handler/response"
	"github.com/super-team/start-kit/models"
)

func Route(r *gin.RouterGroup, middlewares ...gin.HandlerFunc) {

	a := r.Group("/roleMenu", middlewares...)
	{
		a.GET("/:id", getRoleMenu)
	}

}

// @Summary 获取角色绑定的菜单
// @Description 获取角色绑定的菜单
// @Tags roleMenu
// @Accept json
// @Param Authorization header string false "Bearer 用户令牌"
// @Success 200 {string} string	"{"code": 200, "message": "获取数据成功"}"
// @Failure 200 {string} string	"{"code": -1, "message": "参数错误"}"
// @Router /api/roleMenu/:roleId [get]
func getRoleMenu(ctx *gin.Context) {
	id := ctx.Param("id")
	idInt, err := strconv.ParseInt(id, 10, 64)
	if err != nil {
		response.Error(ctx, "参数错误")
		return
	}
	tree, err := models.GetMenuTree(ctx)
	if response.ServerError(ctx, err) {
		return
	}
	m, err := models.GetRoleMenuByRoleID(ctx, idInt)
	if response.ServerError(ctx, err) {
		return
	}
	response.Success(ctx, "获取数据成功", gin.H{
		"checkedKeys": m,
		"menus":       tree,
	})
}
